#pragma once
#include <cppconn/driver.h>
#include <cppconn/connection.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/resultset.h>
#include <cppconn/metadata.h>
#include <cppconn/resultset_metadata.h>
#include <cppconn/exception.h>
#include <mysql-cppconn/jdbc/cppconn/warning.h>

#include <string>

using namespace sql;

const static std::string DbHost = "tcp://127.0.0.1:3306";
const static std::string User = "chen";
const static std::string Passwd = "12345678";
const static std::string Database = "test";

class Mysql
{
public:
    Mysql()
    {
        driver = get_driver_instance();
        conn = driver->connect(DbHost.c_str(), User.c_str(), Passwd.c_str());
        stmt = conn->createStatement();

        stmt->execute(("use " + Database).c_str());
    }
    ~Mysql()
    {
        delete stmt;
        delete conn;
    }

    void Search()
    {
        //std::unique_ptr<ResultSet> res = std::make_unique<ResultSet>();
        ResultSet* res;
        res = stmt->executeQuery("select * from users");
        while(res->next())
        {
            int id = res->getInt(1);
            std::string name = res->getString("name");

            std::cout << "id:" << id << " name:" << name << std::endl;
        }
    }

private:
    sql::Driver *driver;
    sql::Connection *conn;
    sql::Statement *stmt;
};